﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotNet.Core.Entity;
using DotNet.Core.Service;
using DotNet.Helper;
using DotNet.Utility;
using DotNet.Extensions;
using DotNet.JCore.Service;
using DotNet.JCore.Entity;
using DotNet.Auth.Utility;

namespace DotNet.Web.Controllers
{
    public class SystemRegController : BaseController
    {
        private SystemRegService service = new SystemRegService();

        public ActionResult Index()
        {
            var currentUser = AuthHelper.GetSessionUser();
            if (!currentUser.IsAdmin)
            {
                ViewBag.pointId = CurrentEmployee.PointId;
            }
            return View();
        }

        public ActionResult Grid(string name, string Point, string Station, string CheckStatus,string CreateId, DateTime? startDate, DateTime? endDate)
        {
            var list = service.GetPageList(PageInfo(), name, Point, Station, CheckStatus.ToBoolOrNull(), CreateId, startDate.ToDateTimeOrNull(), endDate.ToDateTimeOrNull());
            return View(list);
        }

        public ActionResult Create()
        {
            MarkCreate();
            return EditCore(new SystemReg
            {
                Id = StringHelper.Guid()
            });
        }

        public ActionResult Edit(string id)
        {
            var entity = service.Get(id);
            if (entity == null) return NotFound(id);
            return EditCore(entity);
        }

        private ActionResult EditCore(SystemReg entity)
        {
            return View("Edit", entity);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Save(SystemReg entity)
        {
            AuthHelper.SetJCUserInfo(entity, IsCreate);
            var result = service.Save(entity, IsCreate);
            return Json(result);
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            service.Delete(id.SplitToArray());
            return Json(BoolMessage.True);
        }

        public ActionResult Details(string id)
        {
            var entity = service.Get(id);
            if (entity == null) return NotFound(id);
            return View(entity);
        }

        public ActionResult Export(string name, string Point, string Station, string CheckStatus, string createname, DateTime? startDate, DateTime? endDate)
        {
            return Export(service.GetList(name, Point, Station, CheckStatus.ToBoolOrNull(),createname, startDate.ToDateTimeOrNull(), endDate.ToDateTimeOrNull()));
        }

        private ActionResult NotFound(string id)
        {
            return NotFound("获取 系统稽查登记信息 错误", $"无法找到 主键 = {id} 的系统稽查登记信息");
        }

        /// <summary>
        ///根据稽查点查询稽查人员（二级联动）
        /// </summary>
        /// <param name="PointID">稽查点</param>
        /// <returns></returns>
        public JsonResult GetCheckPointUserInfo(string PointID)
        {
            List<SelectListItem> items = new List<SelectListItem>();
            if (!string.IsNullOrEmpty(PointID))
            {
                var emp = new EmployeeService().GetByPointId(PointID);
                foreach (Employee employees in emp)
                {
                    items.Add(new SelectListItem()
                    {
                        Text = string.Concat(employees.Name),
                        Value = employees.UserId.ToString()
                    });
                }
            }
            return Json(items, JsonRequestBehavior.AllowGet);
        }
    }
}